package com.dxtan90.strategy.impl;

import com.dxtan90.strategy.BaseStrategy;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author shijunhao
 * @since 2024/4/9 10:56
 */

public class Qishuzw extends BaseStrategy {

    @Override
    public String getSourceUrl() {
        return "https://www.qishuzw.com";
    }

    @Override
    public String getCategoryName() {
        return "qishuzw";
    }

    @Override
    public String parseTitle(Document document) {
        return Optional.ofNullable(document.getElementsByClass("d_title"))
                .map(elements -> elements.get(0))
                .map(element -> element.child(0))
                .map(Element::text)
                .orElse("noneTitle_" + System.currentTimeMillis());
    }

    @Override
    public List<String> parseCategory(Document document) {
        Elements lis = Optional.ofNullable(document.getElementById("chapterList"))
                .map(Element::children)
                .orElse(new Elements());
        List<String> category = new ArrayList<>();
        for (Element li : lis) {
            Optional.of(li.child(0)).ifPresent(a -> {
                String href = a.attr("href");
                category.add(href);
            });
        }
        return category;
    }

    @Override
    public List<String> parseContent(Document document) throws IOException {
        List<String> sectionList = new ArrayList<>();
        Elements novels = Optional.ofNullable(document.getElementById("TextContent"))
                .map(article -> article.getElementsByTag("p"))
                .orElse(new Elements());
        novels.forEach(p -> sectionList.add(p.text()));
        return sectionList;
    }

}
